//
// Created by HP on 2024/1/13.
//

#include "aurora_font.h"


Aurora_Font::Aurora_Font(Eflash_Driver *eflash_s){
     __g_xbf_hd = {
            .min = 0x0020,
            .max = 0x9fa0,
            .bpp = 1,
    };
    line_height = 39;
    eflash = eflash_s;
}


void Aurora_Font::addEFLASH_Driver(Eflash_Driver *eflash_s){
    eflash = eflash_s;
}


uint8_t *Aurora_Font::__user_font_getdata(uint32_t offset, int size){
    //如字模保存在SPI FLASH, SPIFLASH_Read(__g_font_buf,offset,size);
    //如字模已加载到SDRAM,直接返回偏移地址即可如:return (uint8_t*)(sdram_fontddr+offset);
    eflash->read_data(offset,buff,size);
    return buff;
}
//获取字符数据
uint8_t *Aurora_Font::getfontData(FONT_SIZE fsize,uint32_t unicode_letter){
    uint32_t of = (fsize == FONT_SIZE::font_size_big) ? FONT_BIG_BASE_ADDR :
                  (fsize == FONT_SIZE::font_size_middle) ? FONT_MIDDLE_BASE_ADDR : FONT_SMALL_BASE_ADDR; //不同字体的起始地址不同
    line_height = (fsize == FONT_SIZE::font_size_big) ? FONT_BIG_HIGHT :
                  (fsize == FONT_SIZE::font_size_middle) ? FONT_MIDDLE_HIGHT : FONT_SMALL_HIGHT; //不同字体的字高不同
    if( unicode_letter>__g_xbf_hd.max || unicode_letter<__g_xbf_hd.min ) {
        return NULL;
    }
    uint32_t unicode_offset = sizeof(x_header_t)+(  unicode_letter - __g_xbf_hd.min)*4 ;
    uint32_t *p_pos = (uint32_t *)__user_font_getdata(unicode_offset + of, 4);
    if( p_pos[0] != 0 ) {
        uint32_t pos = p_pos[0];
        glyph_dsc_t * gdsc = (glyph_dsc_t*)__user_font_getdata(pos + of, sizeof(glyph_dsc_t));
        font_w   = gdsc->box_w;
        font_h   = gdsc->box_h;
        font_r   = gdsc->r;
        fontofs_x = gdsc->ofs_x;
        fontofs_y = gdsc->ofs_y;
        fontadv_w = gdsc->adv_w;
        font_bpp = __g_xbf_hd.bpp;
        return __user_font_getdata(pos+sizeof(glyph_dsc_t) + of, gdsc->box_w*gdsc->box_h*__g_xbf_hd.bpp/8);
    }
    return NULL;
}

uint8_t *Aurora_Font::getaimimgData(uint32_t offsize,uint8_t *buff,uint32_t len){
    eflash->read_data(AIM_IMG_BASE_ADDR + offsize,buff,len);
    return buff;
}
uint8_t *Aurora_Font::getimgData(uint32_t offsize,uint8_t *buff,uint32_t len){
    eflash->read_data(IMG_BASE_ADDR + offsize,buff,len);
    return buff;
}

void Aurora_Font::set_update_type(uint8_t type){
    _update_type = type;
}
uint8_t Aurora_Font::get_update_type(){
    return _update_type;
}
void Aurora_Font::delete_font_data(FONT_SIZE fsize){
    uint32_t addr = (fsize == FONT_SIZE::font_size_big) ? FONT_BIG_BASE_ADDR :
                    (fsize == FONT_SIZE::font_size_middle) ? FONT_MIDDLE_BASE_ADDR : FONT_SMALL_BASE_ADDR; //不同字体的起始地址不同
    uint8_t i;
    for( i = 0 ;i < 16 ; i++){
        eflash->block_erase(addr);
        addr += eflash->get_block_size();
    }
}

void Aurora_Font::update_font_data(FONT_SIZE fsize,uint32_t addr_offset,uint8_t *data,uint16_t len){
    uint32_t addr = (fsize == FONT_SIZE::font_size_big) ? FONT_BIG_BASE_ADDR :
                    (fsize == FONT_SIZE::font_size_middle) ? FONT_MIDDLE_BASE_ADDR : FONT_SMALL_BASE_ADDR; //不同字体的起始地址不同
    addr += addr_offset;
    eflash->write_data(addr,data,len);
}


void Aurora_Font::set_update_font_type(FONT_SIZE fsize){
    update_font_addr = (fsize == FONT_SIZE::font_size_big) ? FONT_BIG_BASE_ADDR :
                    (fsize == FONT_SIZE::font_size_middle) ? FONT_MIDDLE_BASE_ADDR : FONT_SMALL_BASE_ADDR; //不同字体的起始地址不同
}

void Aurora_Font::set_update_font_data(uint8_t * data,uint16_t len){
    eflash->write_data(update_font_addr,data,len);
    update_font_addr += len;
}


//删除7M地址的空间
void Aurora_Font::delete_aim_img_data(){
    uint32_t addr = AIM_IMG_BASE_ADDR;
    uint8_t i;
    for( i = 0 ;i < 16*7; i++){
        eflash->block_erase(addr);
        addr += eflash->get_block_size();
    }
    usb_printf("aim img data,delete ok\r\n");
    update_aim_img_addr = AIM_IMG_BASE_ADDR;
}
void Aurora_Font::set_update_aim_img_data(uint8_t * data,uint16_t len){
    eflash->write_data(update_aim_img_addr,data,len);
    update_aim_img_addr += len;
}

//删除1M地址的空间
void Aurora_Font::delete_img_data(){
    uint32_t addr = IMG_BASE_ADDR;
    uint8_t i;
    for( i = 0 ;i < 16; i++){
        eflash->block_erase(addr);
        addr += eflash->get_block_size();
    }
    usb_printf("img data,delete ok\r\n");
    update_img_addr = IMG_BASE_ADDR;
}
void Aurora_Font::set_update_img_data(uint8_t * data,uint16_t len){
    eflash->write_data(update_img_addr,data,len);
    update_img_addr += len;
}